(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.revokeStopPayment.createController', createController);
	createController.$inject = [ '$q', '$scope', '$filter', 'Constants', 'formatService', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService','storageService',
			'stopPaymentService', 'dateService', 'arithService', 'commonService'];
	function createController($q, $scope, $filter, Constants, formatService, modalService, gridService, layoutConfig, validatorConfig, authService,storageService, stopPaymentService, dateService, arithService, commonService) {
		var ctrl = this;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.stopPayment = {};
		ctrl.init = function() {
			ctrl.stopPayment.operation = "R";
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.member.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = 'vm.stopPayment.allList';
			authService.create(ctrl.stopPayment).then(function(response) {
				ctrl.stopPayment = response.data;
				
			});
		};
		
	
		ctrl.createSave = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			ctrl.stopPayment.memberList = ctrl.selectedRows;
			return authService.createSave(ctrl.stopPayment).then(saveSuccessful, saveFailed);
		};

		ctrl.createSubmit = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			ctrl.stopPayment.memberList = ctrl.selectedRows;
			return authService.createSubmit(ctrl.stopPayment).then(submitSuccessful, submitFailed);
		};

		function submitSuccessful(response) {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			ctrl.stopPayment.memberList = ctrl.selectedRows;
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.submit' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.revokestoppayment.create',
					'state' : 'apf.revokeStopPayment.create',
					'params' : {}
				}]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function saveSuccessful(response) {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			ctrl.stopPayment.memberList = ctrl.selectedRows;
			modalService.openInfoDialogs({
				'messages' : [ 'title.dialog.successful.save' ]
			});
			if (response.data && response.data.model) {
				angular.extend(ctrl.stopPayment, response.data.model);
			}else if (response.data.task && response.data.task.taskId) {
				authService.saveSuccessfulProcess(response.data.task).then(function(response) {
					var routerInfo = response.data || {}, state = routerInfo.processState;
					delete routerInfo.processState;
					$scope.go(state, routerInfo);
				}, angular.noop);
			}
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}
		
	}
})(angular);
